package com.guguskins.back.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guguskins.back.common.constant.PropConstant;
import com.guguskins.back.common.constant.RedisConstant;
import com.guguskins.back.entity.GuBox;
import com.guguskins.back.entity.GuProp;
import com.guguskins.back.entity.GuUser;
import com.guguskins.back.mapper.GuPropMapper;
import com.guguskins.back.service.GuHenHouseTaskService;
import com.guguskins.back.service.GuPropService;
import com.guguskins.back.service.GuUserService;
import com.guguskins.back.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 道具表 服务实现类
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-05-25
 */
@Service
public class GuPropServiceImpl extends ServiceImpl<GuPropMapper, GuProp> implements GuPropService {

    @Autowired
    private GuPropMapper propMapper;

    @Autowired
    private GuHenHouseTaskService henHouseTaskService;

    @Autowired
    private GuUserService userService;

    @Override
    public List<GuProp> getList(Long userId, Long boxId, Integer id, List<Integer> propIds) {
        List<GuProp> props = propMapper.getList(userId, propIds);
        if (!ObjectUtils.isEmpty(boxId) && boxId > 0) {
            Map<String, String> map = RedisUtil.getAllHash(RedisConstant.PROP_STAND_PREFIX + userId + boxId);
            if (!CollectionUtils.isEmpty(map)) {
                List<GuBox> boxes = map.keySet().stream().map(key -> JSON.parseObject(map.get(key), GuBox.class)).collect(Collectors.toList());
                props.forEach(prop -> {
                    if (ObjectUtils.isEmpty(prop.getPropNum())) {
                        prop.setPropNum(0);
                    }
                    if (prop.getPropId().equals(PropConstant.EXCLUDE_CARD) && prop.getPropNum() >= 1) {
                        if (boxes.stream().anyMatch(box -> (!ObjectUtils.isEmpty(box.getFlag()) && box.getFlag().equals(1)))) {
                            prop.setCanUseNum(0);
                        } else {
                            prop.setCanUseNum(1);
                        }
                    }
                    if (prop.getPropId().equals(PropConstant.CUE_CARD) && prop.getPropNum() >= 1 && !ObjectUtils.isEmpty(id)) {
                        if (!ObjectUtils.isEmpty(id)) {
                            GuBox box = JSON.parseObject(map.get(String.valueOf(id)), GuBox.class);
                            if (!ObjectUtils.isEmpty(box.getCueSameId())) {
                                prop.setPropNum(prop.getPropNum() - 1);
                                prop.setCanUseNum(0);
                            } else {
                                prop.setCanUseNum(1);
                            }
                        } else {
                            prop.setCanUseNum(0);
                        }
                    }
                    if (prop.getPropId().equals(PropConstant.DOUBLE_CARD) && prop.getPropNum() >= 1) {
                        if (!ObjectUtils.isEmpty(id)) {
                            GuBox box = JSON.parseObject(map.get(String.valueOf(id)), GuBox.class);
                            if (box.getIsDouble() > 0) {
                                prop.setPropNum(prop.getPropNum() - 1);
                                prop.setCanUseNum(0);
                            } else {
                                prop.setCanUseNum(1);
                            }
                        } else {
                            prop.setCanUseNum(0);
                        }
                    }
                    if (prop.getPropId().equals(PropConstant.WHITE_CARD) && prop.getPropNum() >= 1) {
                        if (!ObjectUtils.isEmpty(id)) {
                            GuBox box = JSON.parseObject(map.get(String.valueOf(id)), GuBox.class);
                            if (box.getIsWhite() > 0) {
                                prop.setPropNum(prop.getPropNum() - 1);
                                prop.setCanUseNum(0);
                            } else {
                                prop.setCanUseNum(1);
                            }
                        } else {
                            prop.setCanUseNum(0);
                        }
                    }
                });
            }
        }
        return props;
    }

    @Override
    public List<GuProp> getList(List<Integer> propIds) {
        List<GuProp> props = this.list(new LambdaQueryWrapper<GuProp>() {
            {
                in(GuProp::getPropId, propIds);
            }
        });
        props.forEach(prop -> {
            prop.setPropNum(0);
            prop.setCanUseNum(0);
        });
        return props;
    }

    @Override
    public List<GuProp> getListByUserId(Long userId, Long boxId, Integer id, List<Integer> propIds) {
        GuUser user = userService.getById(userId);
        if (!ObjectUtils.isEmpty(user)) {
            return this.getList(userId, boxId, id, propIds);
        } else {
            return this.getList(propIds);
        }
    }

    @Override
    public List<GuProp> getListByWhite(Long userId, List<Integer> propIds) {
        List<GuProp> props = propMapper.getList(userId, propIds);
        props.forEach(prop -> prop.setCanUseNum(0));
        return props;
    }

    @Override
    public List<GuProp> getHenHouseList(Long userId) {
        GuUser user = userService.getById(userId);
        List<GuProp> props;
        if (!ObjectUtils.isEmpty(user)) {
            props = propMapper.getListExist(userId, Arrays.asList(PropConstant.HEN_HOUSE, PropConstant.EXTEND_HEN_HOUSE));
        } else {
            props = propMapper.selectBatchIds(Collections.singletonList(PropConstant.HEN_HOUSE));
            props.forEach(prop -> prop.setPropState(1));
        }
        props.stream()
                .filter(prop -> prop.getPropState().equals(1) && !ObjectUtils.isEmpty(prop.getPropExpirationTime()) && prop.getPropExpirationTime().compareTo(LocalDateTime.now()) <= 0)
                .forEach(prop -> {
                    prop.setPropState(0);
                    prop.updateById();
                });

        //剔除无用过期的
        props = props.stream().filter(prop -> ObjectUtils.isEmpty(prop.getPropExpirationTime()) || prop.getPropExpirationTime().compareTo(LocalDateTime.now()) > 0 || (prop.getPropExpirationTime().compareTo(LocalDateTime.now()) <= 0 && prop.getPropState().equals(2))).collect(Collectors.toList());


        return props;
    }

    @Override
    public List<GuProp> acceleratorCard(Long userId) {
        return propMapper.getList(userId, Collections.singletonList(PropConstant.ACCELERATOR_CARD));
    }
}
